﻿/**

 * Copyright (c) 2015-2016, FastDev 刘强 (fastdev@163.com).

 *

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *      http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

using System;
using System.Diagnostics.CodeAnalysis;
using RazorEngine.Compilation.CSharp;
using RazorEngine.Compilation.VisualBasic;
using System.Security;
using System.IO;
using System.Reflection;
using System.Diagnostics.Contracts;
using System.CodeDom.Compiler;
using System.Security.Principal;
using RazorEngine.Templating;
using System.Security.Permissions;
using System.Linq;
using System.Collections.Generic;
using RazorEngine.Compilation.ReferenceResolver;
using RazorEngine.Compilation;
using RazorEngine;

namespace OF.SmartMVC.Core.Compile
{
    /// <summary>
    /// Provides a default implementation of a <see cref="ICompilerServiceFactory"/>.
    /// </summary>
    [Serializable]
    public class StoreDllCompilerServiceFactory : ICompilerServiceFactory
    {
        private List<Assembly> referAssemblis = null;
        private string template;
        public StoreDllCompilerServiceFactory(List<Assembly> referAssemblis, string template)
        {
            this.referAssemblis = referAssemblis;
            this.template = template;
        }

        #region Methods
        /// <summary>
        /// Creates a <see cref="ICompilerService"/> that supports the specified language.
        /// </summary>
        /// <param name="language">The <see cref="Language"/>.</param>
        /// <returns>An instance of <see cref="ICompilerService"/>.</returns>
        [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
        [SecuritySafeCritical]
        public ICompilerService CreateCompilerService(Language language)
        {
            switch (language)
            {
                case Language.CSharp:
                    return new StoreCSharpDirectCompilerService(referAssemblis, template);
                default:
                    throw new ArgumentException("Unsupported language: " + language);
            }
        }
        #endregion
    }
}